<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="用户" v-bind="validateInfos.userId">
            <a-input v-model:value="formData.userId" placeholder="请输入用户" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="用户手机号" v-bind="validateInfos.userPhone">
            <a-input v-model:value="formData.userPhone" placeholder="请输入用户手机号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="订单号" v-bind="validateInfos.orderNo">
            <a-input v-model:value="formData.orderNo" placeholder="请输入订单号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="商户号" v-bind="validateInfos.mchno">
            <a-input v-model:value="formData.mchno" placeholder="请输入商户号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="应用id" v-bind="validateInfos.appId">
            <a-input v-model:value="formData.appId" placeholder="请输入应用id" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="支付方式" v-bind="validateInfos.wayCode">
	          <j-dict-select-tag v-model:value="formData.wayCode" dictCode="way_code" placeholder="请选择支付方式" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="支付金额(分)" v-bind="validateInfos.amount">
	          <a-input-number v-model:value="formData.amount" placeholder="请输入支付金额(分)" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="客户端ip" v-bind="validateInfos.clientIp">
            <a-input v-model:value="formData.clientIp" placeholder="请输入客户端ip" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="订单标题" v-bind="validateInfos.subject">
            <a-input v-model:value="formData.subject" placeholder="请输入订单标题" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="订单描述" v-bind="validateInfos.body">
            <a-input v-model:value="formData.body" placeholder="请输入订单描述" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="失效时间" v-bind="validateInfos.expiredTime">
		        <a-date-picker placeholder="请选择失效时间"  v-model:value="formData.expiredTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="渠道参数" v-bind="validateInfos.channelExtra">
            <a-input v-model:value="formData.channelExtra" placeholder="请输入渠道参数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="分账模式" v-bind="validateInfos.divisionMode">
	          <j-dict-select-tag v-model:value="formData.divisionMode" dictCode="division_mode" placeholder="请选择分账模式" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="扩展参数" v-bind="validateInfos.extParam">
            <a-input v-model:value="formData.extParam" placeholder="请输入扩展参数" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="请求时间" v-bind="validateInfos.reqTime">
		        <a-date-picker placeholder="请选择请求时间"  v-model:value="formData.reqTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="签名" v-bind="validateInfos.sign">
            <a-input v-model:value="formData.sign" placeholder="请输入签名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="签名类型" v-bind="validateInfos.signType">
	          <j-dict-select-tag v-model:value="formData.signType" dictCode="order_sign_type" placeholder="请选择签名类型" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="异步通知地址" v-bind="validateInfos.notifyUrl">
            <a-input v-model:value="formData.notifyUrl" placeholder="请输入异步通知地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="跳转通知地址" v-bind="validateInfos.returnUrl">
            <a-input v-model:value="formData.returnUrl" placeholder="请输入跳转通知地址" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="订单状态" v-bind="validateInfos.orderState">
	          <j-dict-select-tag v-model:value="formData.orderState" dictCode="order_state" placeholder="请选择订单状态" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="支付数据类型" v-bind="validateInfos.payDataType">
            <a-input v-model:value="formData.payDataType" placeholder="请输入支付数据类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="发起支付数据" v-bind="validateInfos.payData">
            <a-input v-model:value="formData.payData" placeholder="请输入发起支付数据" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="渠道错误码" v-bind="validateInfos.errCode">
            <a-input v-model:value="formData.errCode" placeholder="请输入渠道错误码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="渠道错误描述" v-bind="validateInfos.errMsg">
            <a-input v-model:value="formData.errMsg" placeholder="请输入渠道错误描述" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="支付成功时间" v-bind="validateInfos.successTime">
		        <a-date-picker placeholder="请选择支付成功时间"  v-model:value="formData.successTime" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="业务类型" v-bind="validateInfos.bizType">
	          <j-dict-select-tag v-model:value="formData.bizType" dictCode="order_biz_type" placeholder="请选择业务类型" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="业务数据" v-bind="validateInfos.bizDate">
            <a-input v-model:value="formData.bizDate" placeholder="请输入业务数据" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../WlhyOrder.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    userId: '',   
    userPhone: '',   
    orderNo: '',   
    mchno: '',   
    appId: '',   
    wayCode: '',   
    amount: undefined,
    clientIp: '',   
    subject: '',   
    body: '',   
    expiredTime: '',   
    channelExtra: '',   
    divisionMode: '',   
    extParam: '',   
    reqTime: '',   
    sign: '',   
    signType: '',   
    notifyUrl: '',   
    returnUrl: '',   
    orderState: '',   
    payDataType: '',   
    payData: '',   
    errCode: '',   
    errMsg: '',   
    successTime: '',   
    bizType: '',   
    bizDate: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
